package com.jrx.anytxn.param.controller.product;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.dto.product.PrPenaltyRateTableReq;
import com.jrx.anytxn.param.dto.product.PrPenaltyRateTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.product.IPrPenaltyRateTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 罚息利率表表
 *
 * @author Yangxin
 * @date 2019/7/31
 */
@Api(tags = "页面罚息利率表参数API接口")
@RestController
@RequestMapping(value = "/param/penaltyRates")
public class PenaltyRateTableController {
    private static final Logger logger = LoggerFactory.getLogger(PenaltyRateTableController.class);
    @Autowired
    IPrPenaltyRateTableService iPrPenaltyRateTableService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和罚息利率表ID查询罚息利率表表信息", notes = "根据租户ID和罚息利率表ID查询罚息利率表表信息")
    @GetMapping(value = "/{tenantId}/{penaltyId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "主键", required = true),
            @ApiImplicitParam(name = "penaltyId", value = "主键", required = true)
    })
    public TxnRespResult<PrPenaltyRateTableRes> getById(@NotNull @PathVariable String tenantId, @PathVariable String penaltyId) {
        TxnRespResult<PrPenaltyRateTableRes> result = new TxnRespResult<>();
        PrPenaltyRateTableRes res;
        try {
            res = iPrPenaltyRateTableService.findByTenIdAndPanaltyId(String.valueOf(infoService.getUser().getProjectId()), penaltyId);
            if (res.getIncreaseNum() != null && res.getIncreaseNum().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setIncreaseNum(res.getIncreaseNum().multiply(BigDecimal.valueOf(100)));
            }
            if (res.getTaxRate() != null && res.getTaxRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                res.setTaxRate(res.getTaxRate().multiply(BigDecimal.valueOf(100)));
            }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户Id分页查询罚息利率表表信息", notes = "根据租户Id分页查询罚息利率表表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrPenaltyRateTableRes>> getAllPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {

        TxnRespResult<TxnPage<PrPenaltyRateTableRes>> result = new TxnRespResult<>();
        TxnPage<PrPenaltyRateTableRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = iPrPenaltyRateTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增罚息利率表表信息", notes = "新增罚息利率表表信息")
    @PostMapping("/")
    public TxnRespResult<PrPenaltyRateTableRes> createPenaltyRateTable(@RequestBody PrPenaltyRateTableReq prPenaltyRateTableReq) {
        PrPenaltyRateTableRes res;
        TxnRespResult<PrPenaltyRateTableRes> result = new TxnRespResult<>();
        try {
            prPenaltyRateTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prPenaltyRateTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prPenaltyRateTableReq.getIncreaseNum() != null && prPenaltyRateTableReq.getIncreaseNum().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setIncreaseNum(prPenaltyRateTableReq.getIncreaseNum().divide(BigDecimal.valueOf(100)));
            }
            if (prPenaltyRateTableReq.getTaxRate() != null && prPenaltyRateTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setTaxRate(prPenaltyRateTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrPenaltyRateTableService.addPenaltyRateTable(prPenaltyRateTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "编辑罚息利率表表信息", notes = "编辑罚息利率表表信息")
    @PutMapping("/")
    public TxnRespResult<PrPenaltyRateTableRes> modifyPenaltyRateTable(@RequestBody PrPenaltyRateTableReq prPenaltyRateTableReq) {
        TxnRespResult<PrPenaltyRateTableRes> result = new TxnRespResult<>();
        PrPenaltyRateTableRes res;
        try {
            if (prPenaltyRateTableReq.getIncreaseNum() != null && prPenaltyRateTableReq.getIncreaseNum().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setIncreaseNum(prPenaltyRateTableReq.getIncreaseNum().divide(BigDecimal.valueOf(100)));
            }
            if (prPenaltyRateTableReq.getTaxRate() != null && prPenaltyRateTableReq.getTaxRate().compareTo(BigDecimal.ZERO) != 0) {
                prPenaltyRateTableReq.setTaxRate(prPenaltyRateTableReq.getTaxRate().divide(BigDecimal.valueOf(100)));
            }
            if (prPenaltyRateTableReq.getCastPenaltyPerMonth() != null && prPenaltyRateTableReq.getCastPenaltyPerMonth().equals(Constant.DISABLED)){
                prPenaltyRateTableReq.setPenaltyAccrualCompound(Constant.DISABLED);
                prPenaltyRateTableReq.setCompoundAccrualCompound(Constant.DISABLED);
            }
            prPenaltyRateTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = iPrPenaltyRateTableService.modifyPenaltyRateTable(prPenaltyRateTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和罚息利率表ID删除罚息利率表表信息", notes = "根据租户ID和罚息利率表ID删除罚息利率表表信息")
    @DeleteMapping(value = "/{tenantId}/{penaltyId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户Id", required = true),
            @ApiImplicitParam(name = "penaltyId", value = "罚息利率表ID", required = true)
    })
    public TxnRespResult<Boolean> removeById(@NotNull @PathVariable String tenantId, @PathVariable String penaltyId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iPrPenaltyRateTableService.removeByTenIdAndPenaltyId(String.valueOf(infoService.getUser().getProjectId()), penaltyId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

}
